﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;

namespace WuxiWTO.Web
{
    public partial class articleList : System.Web.UI.Page
    {
        WTODataContext ctx = new WTODataContext();
        PagedDataSource psd = new PagedDataSource();

        private string parent_category_id;
        private string category_id;

        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (!IsPostBack)
            {
                SetExtendParame();
            }
            GetExtraParame();
            BuildParentCategoryList();
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        private void SetExtendParame()
        {
            ViewState["pid"] = Request.QueryString["pid"];
            ViewState["cid"] = Request.QueryString["cid"];
        }

        private void GetExtraParame()
        {
            parent_category_id = ViewState["pid"] == null ? "" : ViewState["pid"].ToString();
            category_id = ViewState["cid"] == null ? "" : ViewState["cid"].ToString();
        }

        private void BindGrid()
        {
            category cat;
            string nextURL = "";
            if (!string.IsNullOrEmpty(parent_category_id))
            {
                int intPid = int.Parse(parent_category_id);
                nextURL = "articledetails.aspx?s=y&pid=" + parent_category_id;
                var query = (from c in ctx.articles orderby c.update_date descending where c.parent_category_id == intPid select c).AsQueryable();
                if (!string.IsNullOrEmpty(category_id))
                {
                    int cid = int.Parse(category_id);
                    nextURL += "&cid=" + category_id;
                    cat = ctx.categories.SingleOrDefault(p => p.art_kind_id == cid);

                    query = query.Where(p => p.category_id == cid);
                }
                else
                {
                    cat = ctx.categories.SingleOrDefault(p => p.art_kind_id == intPid);
                }
                //如果该模块为单一页面,则直接跳转到相应的详细页面
                if (cat.single_page == true)
                {
                    Response.Redirect(nextURL);
                    return;
                }

                List<article> list = query.ToList();
                AspNetPager1.RecordCount = list.Count; ;
                psd.DataSource = list;
                psd.AllowPaging = true;
                psd.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                psd.PageSize = AspNetPager1.PageSize;

                this.rptArticle.DataSource = psd;
                this.rptArticle.DataBind();
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "paraerr", "alert('参数传递错误');location='default.aspx';", true);
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        #region 构筑左边的二级菜单以及导航面包屑
        private void BuildParentCategoryList()
        {
            string strParentCategory = "";

            if (!string.IsNullOrEmpty(parent_category_id))
            {
                int intPid = int.Parse(parent_category_id);
                //导航栏项目
                var parent = ctx.categories.SingleOrDefault(p => p.art_kind_id == intPid);
                if (parent != null)
                {
                    //URL不为空的话，点击就进入设置好的URL页面
                    if (!string.IsNullOrEmpty(parent.url))
                    {
                        litBreakCrumbs.Text = string.Format("<a href=\"{0}\">{1}</a>", parent.url, parent.alias == "" ? parent.art_kind_name : parent.alias);
                    }
                    else
                    {
                        litBreakCrumbs.Text = string.Format("<a href=\"articleList.aspx?pid={0}\">{1}</a>", parent.art_kind_id, parent.alias == "" ? parent.art_kind_name : parent.alias);
                    }
                    litCategoryName.Text = parent.alias == "" ? parent.art_kind_name : parent.alias;
                }
                //如果子级类别不为空。则继续构筑导航
                if (!string.IsNullOrEmpty(category_id))
                {
                    int intCid = int.Parse(category_id);
                    var child = ctx.categories.SingleOrDefault(p => p.art_kind_id == intCid);
                    if (child != null)
                    {
                        if (!string.IsNullOrEmpty(child.url))
                        {
                            litBreakCrumbs.Text += string.Format("<span>&gt;&gt;</span><a href=\"{0}\">{1}</a>", child.url, child.alias == "" ? child.art_kind_name : child.alias);
                        }
                        else
                        {
                            litBreakCrumbs.Text += string.Format("<span>&gt;&gt;</span><a href=\"articleList.aspx?pid={0}&cid={1}\">{2}</a>", child.art_parent_kind, child.art_kind_id, child.alias == "" ? child.art_kind_name : child.alias);
                        }
                        litCategoryName.Text = child.alias == "" ? child.art_kind_name : child.alias;
                    }
                }
                //构建左边的二级菜单
                var list = ctx.categories.OrderBy(p=>p.sort_num).Where(p => p.art_parent_kind == intPid).ToList();
                foreach (category cat in list)
                {
                    if (!string.IsNullOrEmpty(cat.url))
                    {
                        strParentCategory += string.Format("<li><a href='{0}'>{1}</a></li>", cat.url, cat.art_kind_name);
                    }
                    else
                    {
                        strParentCategory += string.Format("<li><a href='articleList.aspx?pid={0}&cid={1}'>{2}</a></li>", cat.art_parent_kind, cat.art_kind_id, cat.art_kind_name);
                    }
                }
                //如果为空，表示点击的一级分类下面没有二级分类
                if (string.IsNullOrEmpty(strParentCategory))
                {
                    if (!string.IsNullOrEmpty(parent.url))
                    {
                        strParentCategory = string.Format("<li><a href='{0}'>{1}</a></li>", parent.url, parent.alias == "" ? parent.art_kind_name : parent.alias);
                    }
                    else
                    {
                        strParentCategory = string.Format("<li><a href='articleList.aspx?pid={0}'>{1}</a></li>", parent.art_kind_id, parent.alias == "" ? parent.art_kind_name : parent.alias);
                    }
                }
                this.litParentCategoryName.Text = parent.alias == "" ? parent.art_kind_name : parent.alias;
                this.litParentCategoryList.Text = strParentCategory;
            }
        }
        #endregion
    }
}